package org.csu.jpetstore_front.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.jpetstore_front.common.CommonResponse;
import org.csu.jpetstore_front.entity.*;
import org.csu.jpetstore_front.persistence.*;
import org.csu.jpetstore_front.service.OrderService;
import org.csu.jpetstore_front.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CartItemMapper cartItemMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private LineItemMapper lineItemMapper;

    @Autowired
    private CartShopMapper cartShopMapper;


    @Override
    public int getNextId(String name) {
        return 0;
    }

    @Override
    public CommonResponse<List<CartVO>> getCartShopItems(Account account) {
        QueryWrapper<Cartshop> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",account.getUsername());
        List<Cartshop> cartshopList=cartShopMapper.selectList(queryWrapper);
        List<CartVO> cartVOList=new ArrayList<>();
        for (Cartshop cartshop:cartshopList){
            QueryWrapper<Item> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("itemid",cartshop.getItemId());
            List<Item> itemList=itemMapper.selectList(queryWrapper1);
                for (Item item:itemList){
                    Product product=productMapper.selectById(item.getProductId());
                   CartVO cartVO=toCartVO(cartshop,item,product);
                   cartVOList.add(cartVO);
                }
        }
        return CommonResponse.createForSuccess(cartVOList);
    }

    private CartVO toCartVO(Cartshop cartshop,Item item,Product product){
        CartVO cartVO=new CartVO();
        cartVO.setItemId(cartshop.getItemId());
        cartVO.setQuantity(cartshop.getQuantity());
        cartVO.setProductId(cartshop.getProductId());
        cartVO.setUserName(cartshop.getUserName());

        cartVO.setListPrice(item.getListPrice());
        cartVO.setUnitCost(item.getUnitCost());
        cartVO.setSupplierId(item.getSupplierId());
        cartVO.setStatus(item.getStatus());
        cartVO.setAttribute1(item.getAttribute1());
        cartVO.setAttribute2(item.getAttribute2());
        cartVO.setAttribute3(item.getAttribute3());
        cartVO.setAttribute4(item.getAttribute4());
        cartVO.setAttribute5(item.getAttribute5());

        cartVO.setName(product.getName());
        return cartVO;
    }


    private int newOrderId(){
        List<Order> orderList=orderMapper.selectList(null);
        int k=orderList.size();
        return k+1000;
    }

    @Override
    public CommonResponse<Integer> insertOrder(Order order,List<OrderVO> orderVOList) {
        LineItem lineItem=new LineItem();
        for (OrderVO orderVO:orderVOList) {
            lineItem.setItemId(orderVO.getItemId());
            lineItem.setOrderId(newOrderId());
            lineItem.setQuantity(orderVO.getQuantity());
            lineItem.setUnitPrice(order.getTotalPrice());
            lineItem.setLineNumber(orderVOList.size());
            lineItemMapper.insert(lineItem);        }
        order.setOrderId(newOrderId());
        orderMapper.insert(order);
        return CommonResponse.createForSuccess(order.getOrderId());
    }

    @Override
    public CommonResponse<List<OrderVO>> getOrder(int orderId) {
        QueryWrapper<LineItem> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("orderid",orderId);
        List<LineItem> lineItemList=lineItemMapper.selectList(queryWrapper);
        if(lineItemList.isEmpty()){
            return CommonResponse.createForErrorMessage("无商品");
        }
        Order order=orderMapper.selectById(orderId);
        List<OrderVO> orderVOList=new ArrayList<>();
        for(LineItem lineItem:lineItemList){
            OrderVO orderVO=orderToOrderVO(order,lineItem);
          orderVOList.add(orderVO);
        }
        return  CommonResponse.createForSuccess(orderVOList);
    }

    private OrderVO orderToOrderVO(Order order,LineItem lineItem){
        OrderVO orderVO=new OrderVO();
        orderVO.setOrderId(order.getOrderId());
        orderVO.setItemId(lineItem.getItemId());
        orderVO.setUsername(order.getUsername());
        orderVO.setOrderDate(order.getOrderDate());
        orderVO.setShipAddress1(order.getShipAddress1());
        orderVO.setShipAddress2(order.getShipAddress2());
        orderVO.setShipCity(order.getShipCity());
        orderVO.setShipState(order.getShipState());
        orderVO.setShipZip(order.getShipZip());
        orderVO.setShipCountry(order.getShipCountry());
        orderVO.setBillAddress1(order.getBillAddress1());
        orderVO.setBillAddress2(order.getBillAddress2());
        orderVO.setBillCity(order.getBillCity());
        orderVO.setBillCity(order.getBillCity());
        orderVO.setBillState(order.getBillState());
        orderVO.setCourier(order.getCourier());
        orderVO.setTotalPrice(order.getTotalPrice());
        orderVO.setBillToFirstName(order.getBillToFirstName());
        orderVO.setBillToLastName(order.getBillToLastName());
        orderVO.setShipToFirstName(order.getShipToFirstName());
        orderVO.setShipToLastName(order.getShipToLastName());
        orderVO.setCreditCard(order.getCreditCard());
        orderVO.setExpiryDate(order.getExpiryDate());
        orderVO.setCardType(order.getCardType());
        orderVO.setLocale(order.getLocale());
        orderVO.setStatus(order.getStatus());

        orderVO.setLineNumber(lineItem.getLineNumber());
        orderVO.setQuantity(lineItem.getQuantity());
        orderVO.setItemId(lineItem.getItemId());
        orderVO.setUnitPrice(lineItem.getUnitPrice());

        return orderVO;
    }


    @Override
    public CommonResponse<List<OrderVO>> getOrderByUsername(String username) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",username);
        List<Order> orderList=orderMapper.selectList(queryWrapper);
        if(orderList.isEmpty()){
            return CommonResponse.createForErrorMessage("无商品");
        }
        List<OrderVO> orderVOList=new ArrayList<>();
        for(Order order:orderList) {
            QueryWrapper<LineItem> queryWrappers = new QueryWrapper<>();
            queryWrappers.eq("orderid", order.getOrderId());
            List<LineItem> lineItemList = lineItemMapper.selectList(queryWrappers);
            for (LineItem lineItem1 : lineItemList) {
                OrderVO orderVO = orderToOrderVO(order, lineItem1);
                orderVOList.add(orderVO);
            }
        }
        return  CommonResponse.createForSuccess(orderVOList);
    }






//    @Override
//    public CommonResponse<List<OrderVO>> getCartShopItems(Account account) {
//        QueryWrapper<Cartshop> queryWrapper = new QueryWrapper<>();
//        System.out.println("!!!"+account.getUsername());
//        queryWrapper.eq("userId", account.getUsername());
//        List<Cartshop> cartshopList = cartShopMapper.selectList(queryWrapper);
//
//        List<Item> itemList = null;
//        for (Cartshop cartshop : cartshopList) {
//            QueryWrapper<Item> queryWrapper1 = new QueryWrapper<>();
//            queryWrapper.eq("itemId", cartshop.getItemId());
//            itemList =itemMapper.selectList(queryWrapper1);
//
//            if (itemList.isEmpty()) {
//                return CommonResponse.createForErrorMessage("无");
//            }
//               for (Item item : itemList) {
//                itemList.add(item);
//            }
//        }
//        return CommonResponse.createForSuccess();
//    }

    @Override
    public CommonResponse<OrderVO> insertIntoCartShop(OrderVO orderVO) {
       if(orderVO!=null){
        Cartshop cartshop=new Cartshop();
       cartshop.setItemId(orderVO.getItemId());
       cartshop.setQuantity(orderVO.getQuantity());
       cartshop.setUserName(orderVO.getUsername());
       cartshop.setProductId(orderVO.getProductId());
       cartShopMapper.insert(cartshop);
       return CommonResponse.createForSuccessMessage("已添加到购物车");
    }
    else
        return CommonResponse.createForErrorMessage("服务器出现错误");
    }

    @Override
    public CommonResponse<OrderVO> deleteItemFromCartShop(OrderVO orderVO) {
        if(orderVO!=null){
            QueryWrapper<Cartshop> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("userId",orderVO.getUsername()).eq("itemId",orderVO.getItemId()).eq("productId",orderVO.getProductId()).eq("quantity",orderVO.getQuantity());
           List<Cartshop> cartshopList=cartShopMapper.selectList(queryWrapper);
            if (cartshopList.isEmpty()){
                return CommonResponse.createForErrorMessage("无该订单");
            }
            cartShopMapper.delete(queryWrapper);
            return CommonResponse.createForSuccessMessage("已从购物车移除");
        }
        else
            return CommonResponse.createForErrorMessage("服务器出现错误");
    }

    @Override
    public CommonResponse<OrderVO> updateItemQuantity(OrderVO orderVO) {
        if(orderVO!=null){
            Cartshop cartshop=new Cartshop();
            cartshop.setItemId(orderVO.getItemId());
            cartshop.setQuantity(orderVO.getQuantity());
            cartshop.setUserName(orderVO.getUsername());
            cartshop.setProductId(orderVO.getProductId());
            QueryWrapper<Cartshop> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("userId",orderVO.getUsername()).eq("itemId",orderVO.getItemId()).eq("productId",orderVO.getProductId());
            List<Cartshop> cartshopList=cartShopMapper.selectList(queryWrapper);
            if (cartshopList.isEmpty()){
                return CommonResponse.createForErrorMessage("无该订单");
            }
            cartShopMapper.update(cartshop,queryWrapper);
            return CommonResponse.createForSuccessMessage("数量已更改");
        }
        else
            return CommonResponse.createForErrorMessage("服务器出现错误");

    }

    @Override
    public CommonResponse<AccountVO> deleteAll(AccountVO accountVO) {
        if(accountVO!=null){
            cartShopMapper.deleteById(accountVO.getUsername());
            return CommonResponse.createForSuccessMessage("已清空购物车");
        }
        else
            return CommonResponse.createForErrorMessage("服务器出现错误");
    }

    @Override
    public CommonResponse<List<CCartVO>> deletecartshop(List<CCartVO> orderList) {
        for (CCartVO cCartVO:orderList){
            QueryWrapper<Cartshop> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("userId",cCartVO.getUserName()).eq("itemId",cCartVO.getItemId()).eq("productId",cCartVO.getProductId()).eq("quantity",cCartVO.getQuantity());
            cartShopMapper.delete(queryWrapper);
        }
        return CommonResponse.createForSuccess();
    }
}